package com.wn.web;


import com.fasterxml.jackson.databind.json.JsonMapper;
import com.github.pagehelper.PageInfo;
import com.wn.annotation.RequireRoles;
import com.wn.auth.JWTUtil;
import com.wn.common.Const;
import com.wn.common.PageParam;
import com.wn.ov.ConsultationOv;
import com.wn.ov.PatientOv;
import com.wn.ov.UserOv;
import com.wn.pojo.*;
import com.wn.pojo.Appointment;
import com.wn.pojo.CostDrug;
import com.wn.pojo.MedicalAdvice;
import com.wn.resopnse.ResponseCode;
import com.wn.resopnse.ServerResponse;
import com.wn.service.CostService;
import com.wn.service.HospitalizedApplicationService;
import com.wn.service.UserService;
import com.wn.service.doctorService.ConsultationService;
import com.wn.service.doctorService.DoctorService;
import com.wn.service.doctorService.MedicalAdviceService;
import com.wn.service.doctorService.MedicalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;

@RestController
@RequestMapping("doctor")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private HospitalizedApplicationService hospitalizedApplicationService;
    @Autowired
    private MedicalAdviceService medicalAdviceService;
    @Autowired
    private CostService costService;
    @Autowired
    private ConsultationService consultationService;

    @Autowired
    private UserService userService;
    //病历service
    @Autowired
    private MedicalRecordService medicalRecordService;


    /**
     * 获取该医生的病人
     *
     * @return
     */
    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("getPatientById")
    public ServerResponse getPatientById(@RequestHeader(value = "token") String token, @RequestBody PageParam<PatientOv> newsinfoPageParam) {
//        //解析token
        UserOv user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        PageInfo pageInfo = doctorService.getPatientById(user.getId(), newsinfoPageParam);
        return new ServerResponse(ResponseCode.SUCCESS, pageInfo);
    }

    /**
     * 医嘱crud
     *
     * @return
     */
    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("addMedicalAdvice")
    public ServerResponse addMedicalAdvice(@RequestHeader(value = "token") String token,
                                           @RequestBody @Validated MedicalAdvice medicalAdvice) {
        //解析token
        UserOv user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        medicalAdvice.setCreateUserId(user.getId());
        ServerResponse serverResponse = medicalAdviceService.addMedicalAdvice(medicalAdvice);
        return serverResponse;
    }

    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("updateMedicalAdvice")
    public ServerResponse updateMedicalAdvice(@RequestHeader(value = "token") String token,
                                              @RequestBody @Valid MedicalAdvice medicalAdvice) {
        //解析token
        UserOv user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        medicalAdvice.setUpdateUserId(user.getId());
        ServerResponse serverResponse = medicalAdviceService.updateMedicalAdvice(medicalAdvice);
        return serverResponse;
    }

    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("getMedicalAdvice")
    public ServerResponse getMedicalAdvice(@RequestHeader(value = "token") String token, @RequestBody Map<String, Object> map) {
        ServerResponse serverResponse = medicalAdviceService.getMedicalAdvice(map);
        return serverResponse;
    }

    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("deleteMedicalAdvice")
    public ServerResponse deleteMedicalAdvice(@RequestHeader(value = "token") String token, @RequestBody MedicalAdvice medicalAdvice) {
        ServerResponse serverResponse = medicalAdviceService.deleteMedicalAdvice(medicalAdvice);
        return serverResponse;
    }

    /**
     * 药品清单crud
     *
     * @param
     * @return
     */
//    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("getDrugList")
    public ServerResponse getDrugList(@RequestHeader(value = "token") String token, @RequestBody Map<String, Object> map) {

        ServerResponse serverResponse = costService.getDrugList(map);
        return serverResponse;
    }

    @PostMapping("updateDrugList")
    public ServerResponse updateDrugList(@RequestHeader(value = "token") String token, @RequestBody Map<String, Object> map) {
        //解析token
        UserOv user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        ServerResponse serverResponse = costService.updateDrugList(map, user);
        return serverResponse;
    }

    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("addCostDrugList")
    public ServerResponse addCostDrugList(@RequestHeader(value = "token") String token, @RequestBody Map<String, Object> map) {
        //解析token
        UserOv user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        ServerResponse serverResponse = costService.addCostDrugList(map, user);
        return serverResponse;
    }

    @PostMapping("deleteDrugList")
    public ServerResponse deleteDrugList(@RequestHeader(value = "token") String token, @RequestBody Map<String, Object> map) {
        ServerResponse serverResponse = costService.deleteDrugList(map);
//        costService.updataTotal(Integer.parseInt(patientId));
        return serverResponse;
    }

//    /**
//     * 审核预约
//     *
//     * @param token
//     * @param appointment
//     * @return
//     */
//    @PostMapping("updateAppointment")
//    public ServerResponse updateAppointment(@RequestHeader(value = "token") String token, Appointment appointment) {
//        //解析token
//        UserOv user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
//        ServerResponse serverResponse = userService.updateAppoint(appointment, user.getId());
//
//        return serverResponse;
//    }

    /**
     * 检查、住院crud
     *
     * @param
     * @return
     */
    @PostMapping("getOtherList")
    public ServerResponse getOtherList(@RequestHeader(value = "token") String token, @RequestBody Map<String, Object> map) {
        //解析token
        ServerResponse serverResponse = costService.getOtherList(map);
        return serverResponse;
    }

    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("updateOtherList")
    public ServerResponse updateOtherList(@RequestHeader(value = "token") String token, @RequestBody Map<String, Object> map) {
        //解析token
        UserOv user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        ServerResponse serverResponse = costService.updateOtherList(map, user);
        return serverResponse;
    }

    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("addOtherList")
    public ServerResponse addOtherList(@RequestHeader(value = "token") String token, @RequestBody Map<String, Object> map) {
        //解析token
        UserOv user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        ServerResponse serverResponse = costService.addOtherList(map, user);
        return serverResponse;
    }

    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("deleteOtherList")
    public ServerResponse deleteOtherList(@RequestHeader(value = "token") String token, @RequestBody Map<String, Object> map) {
        ServerResponse serverResponse = costService.deleteOtherList(map);
        return serverResponse;
    }

    /**
     * 添加入院申请
     */
    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("addHospitalizedApplication")
    public ServerResponse addHospitalizedApplication(@RequestHeader(value = "token") String token, @RequestBody PatientOv patientOv) {
        //解析token
        UserOv user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        HospitalizedApplication hospitalizedApplication = new HospitalizedApplication();
        hospitalizedApplication.setDoctorId(user.getId());
        hospitalizedApplication.setPatientId(patientOv.getId());
        ServerResponse serverResponse = hospitalizedApplicationService.addHospitalizedApplication(hospitalizedApplication);
        return serverResponse;
    }

    /**
     * 添加出院申请
     */
    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("addHospitalizedApplicationOut")
    public ServerResponse addHospitalizedApplicationOut(@RequestHeader(value = "token") String token, @RequestBody PatientOv patientOv) {
        ServerResponse serverResponse = hospitalizedApplicationService.addHa(patientOv);
        return serverResponse;
    }

    /**
     * 获取申请
     */
    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("getHospitalizedApplication")
    public ServerResponse getHospitalizedApplication(@RequestHeader(value = "token") String token, @RequestBody PatientOv patientOv) {
        ServerResponse serverResponse = hospitalizedApplicationService.getHa(patientOv);
        return serverResponse;
    }

    /**
     * 删除住院申请
     */
    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("delHospitalizedApplicationOut")
    public ServerResponse delHospitalizedApplicationOut(@RequestHeader(value = "token") String token, @RequestBody PatientOv patientOv) {
        ServerResponse serverResponse = hospitalizedApplicationService.delHospitalizedApplicationOut(patientOv);
        return serverResponse;
    }

    /**
     * 病历增删改查
     */
    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("addMedicalRecord")
    public ServerResponse addMedicalRecord(@RequestHeader(value = "token") String token, @RequestBody @Validated MedicalRecord medicalRecord) {
        //解析token
        UserOv user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        ServerResponse serverResponse = medicalRecordService.addMedicalRecord(medicalRecord);
        return serverResponse;
    }

    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("getMedicalRecord")
    public ServerResponse getMedicalRecord(@RequestHeader(value = "token") String token, String patientId) {
        ServerResponse serverResponse = medicalRecordService.getMedicalRecord(Integer.parseInt(patientId));
        return serverResponse;
    }

    /**
     * 查询病人的病例(多表连接)
     *
     * @param patientOv
     * @return
     */

    //查询病人的病例(多表连接)
    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("getMedicalRecordOv")
    public ServerResponse getMedicalRecordOv(@RequestHeader(value = "token") String token, @RequestBody PatientOv patientOv) {
        ServerResponse serverResponse = medicalRecordService.getMedicalRecordOv(patientOv);
        return serverResponse;
    }

    @PostMapping("updateMedicalRecord")
    @RequireRoles(roles = "main_doctor,doctor")
    public ServerResponse updateMedicalRecord(@RequestHeader(value = "token") String token, @RequestBody @Validated MedicalRecord medicalRecord) {
        ServerResponse serverResponse = medicalRecordService.updateMedicalRecord(medicalRecord);
        return serverResponse;
    }

    //增加病人的病例详情
    @PostMapping("insertMedicalRecordDetail")
    @RequireRoles(roles = "main_doctor,doctor")
    public ServerResponse insertMedicalRecordDetail(@RequestHeader("token") String token, @RequestBody @Validated MedicalRecordDetail medicalRecordDetail) {
        //解析token
        UserOv user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        medicalRecordDetail.setCreateUserId(user.getId());
        ServerResponse serverResponse = medicalRecordService.insertMedicalRecordDetail(medicalRecordDetail);
        return serverResponse;
    }

    //修改病人的病例详情
    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("updateMedicalRecordDetail")
    public ServerResponse updateMedicalRecordDetail(@RequestHeader("token") String token, @RequestBody @Validated MedicalRecordDetail medicalRecordDetail) {
        //解析token
        UserOv user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        ServerResponse serverResponse = medicalRecordService.updateMedicalRecordDetail(medicalRecordDetail, user.getId());
        return serverResponse;
    }

    //删除病人的病例详情
    @PostMapping("deleteMedicalRecordDetail")
    @RequireRoles(roles = "main_doctor,doctor")
    public ServerResponse deleteMedicalRecordDetail(@RequestHeader("token") String token,@RequestBody MedicalRecordDetail medicalRecordDetail) {
        ServerResponse serverResponse = medicalRecordService.deleteMedicalRecordDetail(medicalRecordDetail);
        return serverResponse;
    }

    /**
     * 获取由该医生发起会诊申请的病人
     *
     * @return
     */
    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("getConsultationByCreatedId")
    public ServerResponse getConsultationByCreatedId(@RequestHeader(value = "token") String token, @RequestBody PageParam<ConsultationOv> pageParam) {
//        //解析token
        UserOv user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        ServerResponse serverResponse = consultationService.getConsultationByCreatedId(user.getId(), pageParam);
        return serverResponse;
    }

    /**
     * 删除会诊申请
     *
     * @return
     */
    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("deleteConsultation")
    public ServerResponse deleteConsultation(@RequestHeader(value = "token") String token, @RequestBody Consultation consultation) {
        ServerResponse serverResponse = consultationService.deleteConsultation(consultation);
        return serverResponse;
    }

    /**
     * 发起会诊申请
     *
     * @return
     */
    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("addConsultation")
    public ServerResponse addConsultation(@RequestHeader(value = "token") String token, @RequestBody @Valid Consultation consultation) {
//        //解析token
        UserOv user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        ServerResponse serverResponse = consultationService.addConsultation(user, consultation);
        return serverResponse;
    }

    /**
     * 修改会诊记录
     *
     * @return
     */
    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("updateConsultation")
    public ServerResponse updateConsultation(@RequestHeader(value = "token") String token, @RequestBody @Validated Consultation consultation) {
//        //解析token
        UserOv user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        ServerResponse serverResponse = consultationService.updateConsultation(consultation);
        return serverResponse;
    }

    /**
     * 开始会诊
     *
     * @return
     */
    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("startConsultation")
    public ServerResponse startConsultation(@RequestHeader(value = "token") String token, @RequestBody Consultation consultation) {
        consultation.setStatus(1);
        ServerResponse serverResponse = consultationService.updateConsultation(consultation);
        return serverResponse;
    }

    /**
     * 结束会诊
     *
     * @return
     */
    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("endConsultation")
    public ServerResponse endConsultation(@RequestHeader(value = "token") String token, @RequestBody Consultation consultation) {
        consultation.setEndTime(new Date());
        consultation.setStatus(2);
        ServerResponse serverResponse = consultationService.updateConsultation(consultation);
        return serverResponse;
    }

    /**
     * 获取由会诊医生的病人
     *
     * @return
     */
    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("getConsultationByDoctorId")
    public ServerResponse getConsultationByDoctorId(@RequestHeader(value = "token") String token, @RequestBody PageParam<ConsultationOv> pageParam) {
//        //解析token
        UserOv user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        ServerResponse serverResponse = consultationService.getConsultationByDoctorId(user.getId(), pageParam);
        return serverResponse;
    }

    /**
     * 病人转科审核
     *
     * @return
     */
    @RequireRoles(roles = "main_doctor,doctor")
    @PostMapping("checkTrans")
    public ServerResponse checkTrans(@RequestHeader(value = "token") String token, @RequestBody DepartmentCode departmentCode) {
//        //解析token
        UserOv user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        departmentCode.setUpdateUserId(user.getId());
        ServerResponse serverResponse = doctorService.checkTrans(departmentCode);
        return serverResponse;
    }
}
